//
// Created by zhuyijun on 2021/9/22.
//

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;
const char *file = "guests.txt";

int main() {

    char ch;
    ifstream fin;
    fin.open(file);
    if (fin.is_open()) {
        cout << "Here are the current contents of the " << file << " file:\n";
        while (fin.get(ch)) {
            cout << ch;
        }
        fin.close();
    }
    ofstream fout(file, ios::out | ios::app);
    if (!fout.is_open()) {
        cerr << "Cant's open " << file << " file for output.\n";
        exit(EXIT_FAILURE);
    }
    cout << "Enter guest names (enter a blank line to quit): \n";
    string name;
    while (getline(cin, name) && name.size() > 0) {
        fout << name << endl;
    }
    fout.close();

    fin.clear();
    fin.open(file);
    if (fin.is_open()) {
        cout << "Here are the current contents of the " << file << " file:\n";
        while (fin.get(ch)) {
            cout << ch;
        }
        fin.close();
    }
    cout << "Done.\n";
    return 0;
}

